﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using WebChatApi.Common.Interface;

namespace WebChatApi.Common
{
    /// <summary>
    /// A model class for saving session information
    /// </summary>
    [DataContract]
    [KnownType(typeof(AuthorityInfo))]    
    public class SessionInfo : AuthorityInfo, IIdentifiable
    {
        /// <summary>
        /// The default cultrue info code
        /// </summary>
        protected const string defaultCultrueInfoCode = "en-US";

        #region Properties

        /// <summary>
        /// Gets or sets the culture code.
        /// <example>e.g.: zh-cn, en-us, zh-tw, en-sg</example>
        /// </summary>
        /// <value>
        /// The culture code.
        /// </value>
       [DataMember]
        public string CultureCode
        {
            get;
            set;
        }

        #endregion

        #region Constructor


        /// <summary>
        /// Initializes a new instance of the <see cref="SessionInfo"/> class.
        /// </summary>
        /// <param name="userKey"></param>
        public SessionInfo(Guid userKey)
            : this(Guid.NewGuid(), userKey)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SessionInfo"/> class.
        /// </summary>
        public SessionInfo()
            : base()
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="SessionInfo"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="userKey">The user key.</param>
        public SessionInfo(Guid key, Guid userKey)
            : base(key, userKey)
        {
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="SessionInfo"/> class.
        /// </summary>
        /// <param name="userKey">The user key.</param>
        /// <param name="token">The token.</param>
        /// <param name="sourceType">Type of the source.</param>
        public SessionInfo(Guid userKey, string token, ApplicationSource sourceType)
            : base(userKey, token, sourceType)
        {
        }

        #endregion

        /// <summary>
        /// Updates the thread culture info.
        /// If no valid value is saved in property <c>CultureCode</c>, use default en-us instead.
        /// </summary>
        public void UpdateThreadCultureInfo()
        {
            Thread.CurrentThread.CurrentCulture = null;
            try
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo(this.CultureCode);
            }
            catch
            {
                //Do nothing here.
            }

            if (Thread.CurrentThread.CurrentCulture == null)
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo(defaultCultrueInfoCode);
            }
        }
    }
}
